<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i>&nbsp;修改分站城市信息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-form ref="form" label-width="150px">
                <el-form-item label="分站站长联系方式：">
                    <el-input v-model="contact" placeholder="请输入联系方式" style="width:400px"></el-input>
                </el-form-item>
                <el-form-item label="接收投稿邮箱：">
                    <el-input v-model="email" placeholder="请输入接收投稿邮箱" style="width:400px"></el-input>
                </el-form-item>
                <el-form-item label="分站地址：">
                    <el-input v-model="address" placeholder="请输入详细地址" style="width:400px"></el-input>
                </el-form-item>
                <el-form-item label="经度获取方式" style="width:400px">
                    示例：腾讯地图 坐标器 （提示：经纬度的,标点符号是分割线；请复制经纬度数字进来）<a href="http://lbs.qq.com/tool/getpoint/index.html"  target="_blank">跳转查看</a>
                    <img src="http://appimg.yinsoso.com/backend/TIM%E6%88%AA%E5%9B%BE20190320134958.png" width="1000px;">
                </el-form-item>
                <el-form-item label="分站地址经纬度：">
                    <el-input v-model="latitude" placeholder="请输入联系方式" style="width:400px"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submit">确认操作</el-button>
                   <el-button  @click="returnList">返回列表</el-button>
                </el-form-item>
            </el-form>
        </div>
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { mapGetters } from 'vuex'
import {postUploadBase64} from '@/api/public';
import {getFenzhanCityInfo,postFenzhanCityUpdate} from '@/api/branch/activity';
export default {
    name:"substationAdd",
    data() {
        return {
            typeId:'1', //当前要删除图片的类型   1封面图 2详情图  3关键词
            idx:0,//当前记录的下标
            delVisible:false,
            fullscreenLoading:false,
            delVisible:false,
            accept: 'image/gif, image/jpeg, image/png, image/jpg', //图片上传类型
            selectedOptions:'',//选中的分类
            address:'',//学院名称
            latitude:'',  //封面图
            contact: '',//正文
            email:'',
            editorOption: {
                placeholder: '请输入详情内容'
            },
            editorContent:'',
            editor_style: {
                width: 'auto',
                height: 'auto'
            }
            
        }
    },
    computed:{
        ...mapGetters(['userInfo']),
    },
    components: {

    },
    //实例创建
    created(){
        console.log(this.$route.query.id)
        this.getNews();
    },
    //方法
    methods: {
        getNews (){
            if(this.$route.query.id && this.$route.query.id!=""){
                getFenzhanCityInfo(this.$route.query.id).then(response=>{
                    console.log(response);
                    if(response.code==200){
                        //获取数据
                        var data = response.data;
                        this.address = data.address;
                        this.contact = data.contact;
                        this.email = data.email;
                        let latitude = data.latitude;
                        let longitude = data.longitude;
                        this.latitude =longitude+","+latitude;

                        //console.log(this.data);
                    }else{
                        this.$notify.error({
                            title: '查询失败',
                            duration:2000,
                            message: '失败原因：'+response.data.msg
                        });
                    }
                })
                
            }
        },
        returnList(){
            this.$router.push('/branch/substation');
        },
        //确定删除图片
        deleteRow(){
            if(this.typeId==1){
                //封面图
                this.headImg="";
            }else if(this.typeId==2){
                
            }else if(this.typeId==3){
                //删除关键词信息
                this.skuProp.splice(this.idx,1);
            }
            
            this.$message.success('删除成功');
            this.delVisible = false;
        },
        //删除封面提图片
        delPicture(){
            //当前记录的下标
            this.delVisible = true;
        },
        //触发封面图 file
        uploadBanner(event){
            var _event = event;
            var _this = this;
            let reader =new FileReader();
            let img1=event.target.files[0];
            let type=img1.type;//文件的类型，判断是否是图片
            let size=img1.size;//文件的大小，判断图片的大小
            const files = event.target;
            if(this.accept.indexOf(type) == -1){
                this.$message({
                    message: '请选择gif,jpeg,png,jpg格式图片上传',
                    type: 'warning'
                });
                return false;
            }
            else if(size>3145728){
                this.$message({
                    message: '请选择3M以内的图片上传',
                    type: 'warning'
                });
                return false;
            }else if (typeof(FileReader) === 'undefined') {
                this.$notify.error({
                    title: '错误',
                    message: '抱歉，你的浏览器不支持 FileReader，请使用现代浏览器操作！'
                });

            }else {
                this.fullscreenLoading = true;
                let reader = new FileReader();
                reader.readAsDataURL(img1);
                reader.onload = function(e) {
                    var base = this.result;
                    // console.log(base);

                    postUploadBase64(base).then(response=>{
                        // console.log(response);
                        if(response.code==200){
                            _this.$message({
                                message: '恭喜你，图片上传成功',
                                type: 'success'
                            });
                            _this.headImg=response.data;

                        }else{
                            _this.$notify.error({
                                title: '上传图片失败',
                                duration:2000,
                                message: '失败原因：'+response.msg
                            });
                        }
                        _this.fullscreenLoading = false;
                    })

                }

            }
        },
        //确认发布
        submit(){
            var reg = new RegExp("^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //邮箱正则表达式
            if(this.contact==""){
                this.$notify.info({title: '提示',message: '请填写分站站长联系方式'});
                return false;
            }
            else if(this.email=="" || !reg.test(this.email)){
                this.$notify.info({title: '提示',message: '请正确填写接收投稿邮箱地址'});
                return false;
            }
            else if(this.address==""){
                this.$notify.info({title: '提示',message: '请填写分站详细地址'});
                return false;
            }else if(this.latitude==""){
                this.$notify.info({title: '提示',message: '请填写分站的经纬度'});
                return false;
            }
            else{
                let param ={}
                param.cityId = this.$route.query.id; //编辑分站ID
                param.contact=this.contact;//分站站长联系方式
                param.email=this.email;//分站站长联系方式
                param.address=this.address;//分站地址
                param.longitude=this.latitude.split(",")[1];//经度
                param.latitude=this.latitude.split(",")[0];//纬度
                // console.log(param)
                //调用提交方法

                this.addSave(param);
                
                
            }
        },
        //提交信息上传
        addSave(param){
            var _this =this;
            postFenzhanCityUpdate(param).then(response=>{
                // console.log(response);
                if(response.code==200){
                    this.$message({
                        message: '恭喜你，操作成功',
                        type: 'success'
                    });
                    setTimeout(function(){
                       _this.$router.push('/branch/city');
                    },1500);

                }else{
                    this.$notify.error({
                        title: '操作失败',
                        duration:2000,
                        message: '失败原因：'+response.msg
                    });
                }

            }).catch(error=>{
                console.log(error);
            });
        },
        
        editorLoad(originalName, resultText) {
            // resultText 服务器端返回的text
            // originalName 上传文件名
            this.$refs.vueWangeditor.insertImg(resultText)
        }, 
        returnList(){
            this.$router.push('/branch/city');
        },
    }
}
</script>
<style>
    .news-editor .wangEditor-container .wangEditor-txt{
        height: 300px !important;
    }
</style>
<style lang="stylus" scoped>
    .el-form-item .el-cascader{
        width :400px;
    }
    .el-cascader-menus .el-cascader-menu{
        width :240px
    }
    .sola-upload{
        .sola-left{
            width :100%;
            height :auto;
            .sola-flex{
                float:left
                width: 360px;
                height: 180px;
                margin-bottom 20px;
                margin-right:15px;
                position:relative;
                .delete{
                    position:absolute;
                    top:2px;
                    right:13px;
                    color: #f56c6c;
                    display:inline-block;
                    padding:5px 5px;
                    .el-alert__icon{font-size:28px;cursor:pointer}
                }
                img {
                    width:100%;
                    height:100%;
                }
            }
        }
        .sola-file-box{
            position:relative;
            .btn-file{
                position:absolute;
                top:0;
                left:0;
                z-index 1;
                opacity:0;
                width :120px;
                height :40px;
                outline: 0;
                background-color :none;
                cursor:pointer;
            }
        }

    }
</style>


